package lv.solcraft.replication.common.client;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import lv.solcraft.replication.common.ReplicationConst;
import net.sf.json.JSONObject;

public class ClientSettings {

    private final JSONObject json;
    private final Set<String> tablesWithStoredProcs = new HashSet<String>();
    private final Set<String> ignoreFields = new HashSet<String>();
    private final Set<String> filterForTables = new HashSet<String>();

    public ClientSettings(JSONObject json) {
        this.json = json;
        for (String table : getTablesWithStoredProcs().split(",")) {
            tablesWithStoredProcs.add(table.toUpperCase().trim());
        }
        for (String field : getIgnoreFields().split(",")) {
            ignoreFields.add(field.toUpperCase().trim());
        }
        for (String field : getFilterForTables().split(",")) {
            filterForTables.add(field.toUpperCase().trim());
        }
    }

    public int getMaxOpersPerSession() {
        if (!json.containsKey("maxOpersPerSession"))
            return 10000;
        return json.getInt("maxOpersPerSession");
    }

    public int getOpersBatchSize() {
        if (!json.containsKey("opersBatchSize"))
            return 100;
        return json.getInt("opersBatchSize");
    }

    public String getIgnoreFields() {
        if (!json.containsKey("ignoreFields"))
            return "";
        return json.getString("ignoreFields");
    }

    public String getFilterForTables() {
        if (!json.containsKey("filterForTables"))
            return "";
        return json.getString("filterForTables");
    }

    public String getBlobFields() {
        if (!json.containsKey("blobFields"))
            return "";
        return json.getString("blobFields");
    }

    private String getTablesWithStoredProcs() {
        if (!json.containsKey("tablesWithStoredProcs"))
            return "";
        return json.getString("tablesWithStoredProcs");
    }

    public String getFilter() {
        if (!json.containsKey("filter"))
            return "";
        return json.getString("filter");
    }

    public boolean useStoredProcsForTable(String table) {
        return getTablesWithStoredProcs().trim().equals("*") || tablesWithStoredProcs.contains(table.toUpperCase().trim());
    }

    public boolean isIgnoredField(String tableName, String field) {
        return ignoreFields.contains(tableName.toUpperCase().trim() + "." + field.toUpperCase().trim());
    }

    public boolean filterTable(String table) {
        return getFilterForTables().trim().equals("*") || filterForTables.contains(table.toUpperCase().trim());
    }

    public int getLogLevel() {
        return ReplicationConst.EVENT_TYPE_INFO;
    }

    public List<String> getBlobFields(String atable, String aupdatedField) {
        List<String> blobFields = new ArrayList<String>();
        String[] entries = getBlobFields().split("[,]");
        for (String entry : entries) {
            if (entry.trim().equals(""))
                continue;
            String[] entrySplit = entry.split("[->]");
            String[] updateFieldSplit = entrySplit[0].split("[.]");
            String table = updateFieldSplit[0].trim().toUpperCase();
            String updateField = updateFieldSplit[1].trim().toUpperCase();
            String blobField = entrySplit[2];
            if (atable.equals(table) && aupdatedField.equals(updateField))
                blobFields.add(blobField);
        }
        return blobFields;
    }
}
